package com.hxkcloud.patient.activitys;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.PerformListAdapter;
import com.hxkcloud.patient.beans.DepartmentOrderBean;
import com.hxkcloud.patient.popup.CalendarPopWindow;
import com.hxkcloud.patient.utils.BrightnessUtils;
import com.hxkcloud.patient.views.ListDividerItemDecoration;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 提醒单列表
 * Created by ray_lei on 2016/7/13.
 */
public class RemindListActivity extends BaseActivity implements View.OnClickListener, PerformListAdapter.ResultSelectedListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_new;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<DepartmentOrderBean> stringInfoList;
    private PerformListAdapter adapter;


    private PopupWindow popup;
    private Button previewBtn;
    private Button deleteBtn;
    private Button nowBtn;
    private Button timerBtn;
    private Button cancelBtn;

    private CalendarPopWindow calendarView;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_remindlist);
        stringInfoList = new ArrayList<DepartmentOrderBean>();
//        stringInfoList.add("康复监测");
//        stringInfoList.add("康复训练");
//        stringInfoList.add("血压监测");
//        stringInfoList.add("血糖监测");

        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_new = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.remindlist_text_title));
        tv_new.setVisibility(View.VISIBLE);
        tv_new.setText(getResources().getText(R.string.performlist_text_create));
        iv_back.setOnClickListener(this);
        tv_new.setOnClickListener(this);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.remindlist_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.remindlist_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);
        ListDividerItemDecoration itemDecoration = new ListDividerItemDecoration(
                this, ListDividerItemDecoration.VERTICAL_LIST);
        recycleView.addItemDecoration(itemDecoration);

        adapter = new PerformListAdapter(this, stringInfoList, recycleView, this);
        recycleView.setAdapter(adapter);

        initPopup();

    }

    private void initPopup() {
        popup = new PopupWindow(this);
        View viewSex = LayoutInflater.from(this).inflate(R.layout.popup_perform_select, null);
        popup.setContentView(viewSex);

        popup.setWidth(screenWidth);
        popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setFocusable(true);
        popup.setTouchable(true);
        popup.setOutsideTouchable(true);
        popup.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup.setAnimationStyle(R.style.popupwindow_anim_default);

        previewBtn = (Button) viewSex.findViewById(R.id.bt_select_preview);
        deleteBtn = (Button) viewSex.findViewById(R.id.bt_select_delete);
        nowBtn = (Button) viewSex.findViewById(R.id.bt_select_now);
        timerBtn = (Button) viewSex.findViewById(R.id.bt_select_timer);
        cancelBtn = (Button) viewSex.findViewById(R.id.bt_select_cancel);

        previewBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        nowBtn.setOnClickListener(this);
        timerBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(RemindListActivity.this);
            }
        });
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        tv_new.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back://back
                finish();
                break;
            case R.id.tv_title_func://新建

                break;
            case R.id.bt_select_preview://预览
                popup.dismiss();
                break;
            case R.id.bt_select_delete://删除
                popup.dismiss();
                break;
            case R.id.bt_select_now://立即使用
                popup.dismiss();
                break;
            case R.id.bt_select_timer://定时使用
                if (calendarView == null) {
                    calendarView = new CalendarPopWindow(this);
                }
                calendarView.setCalendarListener(new CalendarPopWindow.CalendarListener() {
                    @Override
                    public void setCalendar(String calendar) {
                        Toast.makeText(mContext, calendar, Toast.LENGTH_LONG).show();
                    }
                });
                closeInputMethodManager();
//                calendarView.showAtLocation(ll_title, Gravity.BOTTOM, 0, 0);
//                BrightnessUtils.lightOff(RemindListActivity.this);
                calendarView.showBottom(ll_title);
                popup.dismiss();

                break;
            case R.id.bt_select_cancel://取消
                popup.dismiss();
                break;
        }
    }

    @Override
    public void onSelected(String type, DepartmentOrderBean info) {
        if ("click".equals(type)) {
            closeInputMethodManager();
            popup.showAtLocation(ll_title, Gravity.BOTTOM, 0, 0);
            BrightnessUtils.lightOff(RemindListActivity.this);
        }
    }
}
